package com.bdmc.report.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.report.domain.bo.ReportCarMilesBo;
import com.bdmc.report.domain.vo.ReportCarMilesVo;
import com.bdmc.report.domain.ReportCarMiles;
import com.bdmc.report.mapper.ReportCarMilesMapper;
import com.bdmc.report.service.IReportCarMilesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 车辆里程统计Service业务层处理
 *
 * @author bdmc
 * @date 2022-08-10
 */
@Service
public class ReportCarMilesServiceImpl extends ServicePlusImpl<ReportCarMilesMapper, ReportCarMiles, ReportCarMilesVo> implements IReportCarMilesService {

    @Override
    public ReportCarMilesVo queryById(Long milesId){
        return getVoById(milesId);
    }

    @Override
    public TableDataInfo<ReportCarMilesVo> queryPageList(ReportCarMilesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportCarMiles> lqw = buildQueryWrapper(bo);
        Page<ReportCarMilesVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ReportCarMilesVo> mySelectPage(ReportCarMilesBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<ReportCarMilesVo> queryList(ReportCarMilesBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<ReportCarMiles> buildQueryWrapper(ReportCarMilesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportCarMiles> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPersonId() != null, ReportCarMiles::getPersonId, bo.getPersonId());
        lqw.eq(bo.getCarId() != null, ReportCarMiles::getCarId, bo.getCarId());
        lqw.eq(bo.getFenceId() != null, ReportCarMiles::getFenceId, bo.getFenceId());
        lqw.between(params.get("beginDataTime") != null && params.get("endDataTime") != null,
            ReportCarMiles::getDataTime ,params.get("beginDataTime"), params.get("endDataTime"));
        lqw.eq(bo.getCarMiles() != null, ReportCarMiles::getCarMiles, bo.getCarMiles());
        return lqw;
    }

    @Override
    public Boolean insertByBo(ReportCarMilesBo bo) {
        ReportCarMiles add = BeanUtil.toBean(bo, ReportCarMiles.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setMilesId(add.getMilesId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ReportCarMilesBo bo) {
        ReportCarMiles update = BeanUtil.toBean(bo, ReportCarMiles.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportCarMiles entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
